package com.beecode.enzyme.demo.javaserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.omg.CORBA.RepositoryIdHelper;

import com.jiuqi.dna.core.http.DNAHttpServlet;
/**
 * @author zhengjiachao
 *
 */
public class ServerServlet extends DNAHttpServlet {


	/**
	 * 
	 */
	private static final long serialVersionUID = 2674053893390727444L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		doService(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		doService(request, response);
	}
	
	private void doService(final HttpServletRequest request,
			final HttpServletResponse response) throws IOException,
			ServletException {
		request.setCharacterEncoding("UTF-8");
		ServletInputStream inputStream = request.getInputStream();
		StringBuilder sb = new StringBuilder();
		InputStreamReader isr = new InputStreamReader(inputStream,"UTF-8");
		BufferedReader br = new BufferedReader(isr);
		String line = null;
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
		JSONObject json = new JSONObject(sb.toString());
		response.setCharacterEncoding("UTF-8");
		response.getOutputStream().write(("you said " + json.getString("data") + " , thank you~").getBytes("UTF-8") );
	}
}
